package com.cskaoyan.controller.vx;


import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.bo.vxBo.AddressBo;
import com.cskaoyan.bean.bo.vxBo.DeleteAddressBo;
import com.cskaoyan.bean.po.wxPo.MarketAddress;
import com.cskaoyan.bean.po.wxPo.MarketUser;
import com.cskaoyan.bean.vo.TVO;
import com.cskaoyan.bean.vo.vxVo.AddressListVo;
import com.cskaoyan.service.vx.AddressService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author: DYQ
 * @time: Created on 2022/6/7 11:04
 */


@RestController
@RequestMapping("wx/address")
public class AddressController {

    @Autowired
    AddressService addressService;

    @RequestMapping("list")
    public BaseRespVo list() {
        Subject subject = SecurityUtils.getSubject();
        MarketUser marketUser = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Integer id = marketUser.getId();
        TVO<MarketAddress> tvo = addressService.queryAddressList(id);
        return BaseRespVo.ok(tvo);
    }

    @RequestMapping("detail")
    public BaseRespVo detail(Integer id) {
        AddressListVo addressListVo = addressService.queryAddressDetail(id);
        return BaseRespVo.ok(addressListVo);
    }

    @RequestMapping("save")
    public BaseRespVo save(@RequestBody AddressBo addressBo) {

        System.out.println("=================");
        System.out.println(addressBo);
        System.out.println("=================");

        if (!StringUtils.isNumericSpace(addressBo.getTel())) {
            return BaseRespVo.err("参数不对", 401);
        }else if (addressBo.getTel().length() != 11){
            return BaseRespVo.err("参数不对", 401);
        }else {
            Subject subject = SecurityUtils.getSubject();
            MarketUser marketUser = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
            Integer userId = marketUser.getId();
            MarketAddress marketAddress = addressService.addAddress(addressBo, userId);
            return BaseRespVo.ok(marketAddress.getId());
        }
    }

    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody DeleteAddressBo deleteAddressBo) {
        addressService.deleteAddressById(deleteAddressBo.getId());
        return BaseRespVo.ok(null);
    }

}
